<?php

namespace App\Http\Controllers\Admin;

use App\Library\KdClient;
use App\Models\Express;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class OrderController extends BaseController
{
    /**
     * 订单列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index(){

        $this->assign([
            'tab'=>'all',
            'order_no'=>'',
            'seller_name'=>'',
            'buyer_name'=>'',
            'trade_status'=>'',
            'pay_type'=>'',
            'time_begin'=>'',
            'time_end'=>''
        ]);

        $queryParams = [];
        $query = Order::with('items');

        $tab = $this->request->get('tab');
        if ($tab) {
            $queryParams['tab'] = $tab;
            if ($tab == 'waitPay'){
                $query = $query->where('trade_status', 1);
            }elseif ($tab == 'waitSend'){
                $query = $query->where('trade_status', 2);
            }elseif ($tab == 'send'){
                $query = $query->where('trade_status', 3);
            }elseif ($tab == 'received') {
                $query = $query->where('trade_status', 4);
            }elseif ($tab == 'refunding'){
                $query = $query->where('trade_status', 5);
            }elseif ($tab == 'closed'){
                $query = $query->where('trade_status', 6);
            }
        }


        $order_no = $this->request->get('order_no');
        if ($order_no) {
            $queryParams['order_no'] = $order_no;
            $query = $query->where('order_no', $order_no);
        }

        $seller_name = $this->request->input('seller_name');
        if ($seller_name) {
            $queryParams['seller_name'] = $seller_name;
            $query = $query->where('seller_name', $seller_name);
        }

        $buyer_name = $this->request->get('buyer_name');
        if ($buyer_name) {
            $queryParams['buyer_name'] = $buyer_name;
            $query = $query->where('buyer_name', $buyer_name);
        }

        $trade_status = intval($this->request->get('trade_status'));
        if ($trade_status) {
            $queryParams['trade_status'] = $trade_status;
            $query = $query->where('trade_status', $trade_status);
        }

        $pay_type = intval($this->request->get('pay_type'));
        if ($pay_type) {
            $queryParams['pay_type'] = $pay_type;
            $query = $query->where('pay_type', $pay_type);
        }

        $time_begin = $this->request->get('time_begin');
        if ($time_begin){
            $queryParams['time_begin'] = $time_begin;
            $query = $query->where('created_at', '>', strtotime($time_begin));
        }

        $time_end = $this->request->get('time_end');
        if ($time_end){
            $queryParams['time_end'] = $time_end;
            $query = $query->where('created_at', '<', strtotime($time_end));
        }

        $this->assign($queryParams);

        $query = $query->orderByDesc('order_id')->paginate(20);
        $this->assign([
            'pagination'=>$query->appends($queryParams)->render(),
            'orderlist'=>$query->map(function ($order){
                $order->item = $order->items[0];
                return $order;
            }),
            'trade_status_items'=>trans('mall.trade_status_items')
        ]);

        return $this->view('admin.trade.order');
    }

    public function batchDelete(Request $request)
    {
        $orders = $request->input('orders');
        if ($orders)
        {
            Order::whereIn('order_id', $orders)->get()->map(function (Order $order){
                $order->delete();
            });
        }
        return ajaxReturn();
    }

    /**
     * 订单详情
     */
    public function detail(){

        $order_id = intval($this->request->get('order_id'));
        $order = Order::find($order_id);
        $this->assign([
            'order_id'=>$order_id,
            'order'=>$order,
            'shipping'=>$order->shipping,
            'shop'=>$order->shop,
            'itemlist'=>$order->items,
            'shipping_types'=>trans('mall.shipping_types'),
            'invoice'=>$order->invoice
        ]);

        return $this->view('admin.trade.detail');
    }

    public function export()
    {

        $orders = [
            [
                '订单号',
                '产品',
                '单价',
                '数量',
                '卖家',
                '买家',
                '运费',
                '小计',
                '下单时间'
            ]
        ];

        $query = Order::with('items');

        $order_no = $this->request->get('order_no');
        if ($order_no) {
            $query = $query->where('order_no', $order_no);
        }

        $seller_name = $this->request->input('seller_name');
        if ($seller_name) {
            $query = $query->where('seller_name', $seller_name);
        }

        $buyer_name = $this->request->get('buyer_name');
        if ($buyer_name) {
            $query = $query->where('buyer_name', $buyer_name);
        }

        $trade_status = intval($this->request->get('trade_status'));
        if ($trade_status) {
            $query = $query->where('trade_status', $trade_status);
        }

        $pay_type = intval($this->request->get('pay_type'));
        if ($pay_type) {
            $query = $query->where('pay_type', $pay_type);
        }

        $time_begin = $this->request->get('time_begin');
        if ($time_begin){
            $query = $query->where('created_at', '>', strtotime($time_begin));
        }

        $time_end = $this->request->get('time_end');
        if ($time_end){
            $query = $query->where('created_at', '<', strtotime($time_end));
        }

        $query->orderByDesc('order_id')->limit(10000)->get()->map(function (Order $order) use (&$orders){
            $item = $order->items->first();
            $orders[] = [
                "$order->order_no",
                $item->title,
                $item->price,
                $item->quantity,
                $order->shop_name,
                $order->buyer_name,
                $order->shipping_fee,
                $order->total_fee,
                @date('Y-m-d H:i:s', $order->created_at)
            ];
        });

        $collection = new Collection($orders);
        return $collection->downloadExcel(
            'orderTable.xls',
            'Xls',
            false
        );
    }

    public function wuliu(Request $request)
    {
        $express_id = $request->input('express_id');
        $express_no = $request->input('express_no');

        $express = Express::find($express_id);

        $client = new KdClient();
        $client->setShipperCode($express->code);
        $client->setLogisticCode($express_no);
        $traces = $client->sendRequest()->getTraces();

        return $this->view('admin.trade.wuliu', compact('express_id', 'express_no', 'express', 'traces'));
    }
}
